package net.chrisjrock.ui
{

	public class KeyboardControl
	{
		private var _id:String;
		private var _keyCode:int = -1;
		private var _activateHandler:Function;
		private var _deactivateHandler:Function;
		
		private var _enabled:Boolean;
		private var _active:Boolean;
		private var _repeatable:Boolean;
		
		private var _controlScheme:ConfigControlScheme;
		
		public function KeyboardControl()
		{
		}
		
		public function destroyInternal ():void
		{
			_id = null;
			_keyCode = 0;
			_activateHandler = _deactivateHandler = null;
			_enabled = _active = _repeatable = false;
			_controlScheme = null;
		}
		
		public function activate ():void
		{
			if (_enabled && Boolean(_activateHandler) && (!_active || _repeatable))
			{
				_active = true;
				_activateHandler();
			}
		}
		
		public function deactivate ():void
		{
			if (_enabled && Boolean(_deactivateHandler) && _active)
			{
				_active = false;
				_deactivateHandler();
			}
		}
		
		public function toggle ():void
		{
			if (_active)
			{
				deactivate();
			}
			else
			{
				activate();
			}
		}
		
		public function getId ():String
		{
			return _id;
		}
		
		public function setId (value:String):void
		{
			if (_controlScheme)
			{
				_controlScheme.removeControl(this);
			}
			
			_id = value;
			
			if (_controlScheme)
			{
				_controlScheme.addControl(this);
			}
		}
		
		public function getKeyCode ():int
		{
			return _keyCode;
		}
		
		public function setKeyCode (value:int):void
		{
			if (_controlScheme)
			{
				_controlScheme.removeControl(this);
			}
			
			_keyCode = value;
			
			if (_controlScheme)
			{
				_controlScheme.addControl(this);
			}
		}
		
		public function getActivateHandler ():Function
		{
			return _activateHandler;
		}
		
		public function setActivateHandler (value:Function):void
		{
			_activateHandler = value;
		}
		
		public function getDeactivateHandler ():Function
		{
			return _deactivateHandler;
		}
		
		public function setDeactivateHandler (value:Function):void
		{
			_deactivateHandler = value;
		}
		
		public function isEnabled ():Boolean
		{
			return _enabled;
		}
		
		public function setEnabled (value:Boolean):void
		{
			_enabled = value;
		}
		
		public function isActive ():Boolean
		{
			return _active;
		}
		
		public function setActive (value:Boolean):void
		{
			_active = value;
		}
		
		public function isRepeatable ():Boolean
		{
			return _repeatable;
		}
		
		public function setRepeatable (value:Boolean):void
		{
			_repeatable = value;
		}
		
		public function getControlScheme ():ConfigControlScheme
		{
			return _controlScheme;
		}
		
		public function setControlScheme (value:ConfigControlScheme):void
		{
			_controlScheme = value;
		}
	}
}